from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError
from django.core import serializers
from django.utils.datastructures import SortedDict

from optparse import make_option

import os, subprocess
from nutrition.migration import dynamic_import, ARSReader, FORM_DICT, MIGRATION_TASKS

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--key', default='', dest='key', type='str',
            help='Specifies the key of the corresponding csv file.'),
        make_option('--start', default=0, dest='start', type='int',
            help='Specifies the starting row of the csv file.'),
        make_option('--end', default=None, dest='end', type='int',
            help='Specifies the ending row of the csv file'),
        make_option('--encoding', default='latin-1', dest='encoding', type='str',
            help='Specifies the csv file encoding.'),
        make_option('--threshold', default=10000, dest='THRESHOLD', type='int',
            help='Specifies the maximum number of rows for a individual process to handle.'),
    )
    help = 'Import all or part of the sr21 USDA National Nutrient Database.'
    args = '[sr21 dataset directory]'
    
    def handle(self, path, **options):
        if not os.path.exists(path):
            raise CommandError("The path %s does not exist." % path)
        
        key       = options.get('key',       None)
        start     = options.get('start',     0)
        end       = options.get('end',       None)
        encoding  = options.get('encoding',  'latin-1')
        THRESHOLD = options.get('THRESHOLD', 10000)
        PWD       = os.environ['PWD']
        
        if key:
            if key not in FORM_DICT.keys():
                raise CommandError("Invalid key specified. Key choices are: %s" % FORM_DICT.keys())
            value = FORM_DICT[key]
            
            # Dynamically import the corresponding module and get the Form attribute
            module = dynamic_import('nutrition.forms')
            form = getattr(module, value['form_name'])
            
            # Ensure that the path ends with a trailing slash
            path = path.rstrip('/') + '/'
            file_path = path + key + '.txt'
            
            if not os.path.exists(file_path):
                raise CommandError("The file %s does not exist.  Please make sure you leave the extracted sr21 dataset untouched." % file_path)
            
            # Set the fieldnames to a var
            fieldnames = value['fieldnames']
            
            # If an end value is provided, check the length before
            # counting the rows
            if end:
                length = end - start
            else:
                # Open a sanitizing DictReader on the current key
                reader = ARSReader(file_path, fieldnames, encoding, start, end)
                # Determine a row count and delete the reader
                row_count = reader.count()
                del reader
                length = row_count - start
            
            if length > THRESHOLD:
                num_segments = int(round(length / THRESHOLD))
                
                command_set = []
                for multiplier in range(0, num_segments + 1):
                    cmd = [
                        PWD + '/manage.py',
                        'import_sr21',
                        path,
                        '--key=%s' % key,
                        '--start=%s' % (int(start) + THRESHOLD * multiplier),
                        '--end=%s' % (int(start) + THRESHOLD * (multiplier + 1)),
                        '--threshold=%s' % THRESHOLD,
                    ]
                    command_set.append(cmd)
                
                for cmd in command_set:
                    p = subprocess.Popen(cmd)
                    p.wait()
            else:
                # Open a sanitizing DictReader on the current key
                reader = ARSReader(file_path, fieldnames, encoding, start, end)
                print start, end
                for row in reader:
                    populated_form = form(data=row)
                    obj = populated_form.save(commit=False)
                    obj.save()
        else:
            for source in MIGRATION_TASKS:
                print "%s working..." % source
                cmd = [
                    PWD + '/manage.py',
                    'import_sr21',
                    path,
                    '--key=%s' % source,
                    '--threshold=%s' % THRESHOLD,
                ]
                p = subprocess.Popen(cmd)
                p.wait()
                print "%s complete" % source